/**
 * 深拷贝
 * @author zs.duan
 * @date 2024-08-16
 * @param {Record<string, any>} obj 需要拷贝的对象
 * @returns {Record<string, any>} 拷贝后的对象
 * */
function deepCopy<T>(obj: T): T {
    if (Array.isArray(obj)) {
        return (obj as any[]).map(item => deepCopy(item)) as any as T;
    }
    if (typeof obj === 'object' && obj !== null) {
        const newObj: Record<string, any> = {};
        for (const key in obj) {
            if (obj.hasOwnProperty(key)) {
                newObj[key] = deepCopy(obj[key]);
            }
        }
        return newObj as T;
    }
    if(obj instanceof Date){
        return new Date(obj.getTime()) as T;
    }
    if(obj instanceof RegExp){
        return new RegExp(obj) as T;
    }
    // 基本类型直接返回  
    return obj;
}

export default deepCopy;